package personal.xjl._23java1studentdemo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jdk.jfr.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import personal.xjl._23java1studentdemo.mapper.StudentMapper;
import personal.xjl._23java1studentdemo.mapper.TeacherMapper;
import personal.xjl._23java1studentdemo.model.Student;
import personal.xjl._23java1studentdemo.model.Teacher;
import personal.xjl._23java1studentdemo.service.StudentService;

import javax.swing.text.Style;
import java.util.ArrayList;
import java.util.List;


@Controller       //注解，定义一个控制器，相当于的Servlet,
//web.xml,<servlet><name><class></servlet>
@RequestMapping("/Student")//请求映射，定义servlet的访问形式,<servlet-mapping><url-pattern>
public class StudentController {
    //声明使用持久层对象
    @Autowired    //自动装配，注入,spring容器
    StudentService studentService;
    //查
    @RequestMapping("list") //方法的请求映射
    // @ResponseBody     //响应给用户的是body
    public String list(Model model) {
        //调用studentMapper的selectAll方法，获取student表的数据，赋值给students
        List<Student> students=studentService.findAll();
        model.addAttribute("students", students);
        //System.out.println("list");   //在控制台打印字符串list
        return "listStudents";              //转向到/templates/listStudents.html页面
    }
    @RequestMapping("remove")
    public String remove(int id) {
        studentService.remove(id);
        return "redirect:/Student/list/1/3";
    }
    //增加学生
    //删除学生
    //登录
    //转到任意页面的方法,将目标页面名称当做参数传递
    @RequestMapping("toPage")
    public String toPage(String page){
        return page;
    }
    //登录验证
//    @RequestMapping("loginValidate")
//    public String loginValidate(Student student, HttpServletRequest request, Model model, HttpSession session, HttpServletResponse response){
////        System.out.println(student.getName()+" , " + student.getPassword()+",from "+request.getRemoteAddr());
//        //controller,MVC,Model,View,Controller,渲染,将username和password变量数据存到model对象中
//   /*     model.addAttribute("username", username);
//        model.addAttribute("password", password);*/
//        //设置一个会话有效的变量，名字是username,值是student.getUsername()
////        session.setAttribute("username", student.getName());
//        model.addAttribute("student", student);
//        //传list类型的数据
//        List students = new ArrayList();
//        students.add(student);
//        //   students.add(new Student("tom","1",1));
//        //   students.add(new Student("jerry","2",2));
//        model.addAttribute("students", students);
//        //服务器往客户端发送了两对cookie
////        response.addCookie(new Cookie("username", student.getName()));
////        response.addCookie(new Cookie("password", student.getPassword()));
//        return "list";
//    }
    @RequestMapping("add")
    public String add(Student student) {
        studentService.add(student);
        return "redirect:/Student/list/1/3";
    }
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model) {
        Student oldStudent=studentService.findById(id);
        model.addAttribute("student", oldStudent);
        return "editStudent";
    }

    @RequestMapping("edit")
    public String edit(Student student) {
        studentService.edit(student);
        return "redirect:/Student/list/1/3";
    }

    @RequestMapping("list/{currPage}/{pageSize}")
    //分页显示
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize")int pageSize){
        if(currPage<1) currPage=1;
        if (pageSize <=0) pageSize=5;
        List<Student> students = studentService.findByPage(currPage, pageSize);
        model.addAttribute("students", students);
        return "listStudents";
    }
    //变量的生命期有4种：page(默认),request,session(会话,anywhere),application(应用，点击量)
//    @RequestMapping("loginValidate")
//    public String loginValidate(String username, String password, Model model, HttpSession session) {
//        if (studentService.login(username, password)) {
//            session.setAttribute("username", username);
//            return "redirect:/Student/list/1/3";
//        } else {
//            return "redirect:/Student/login";
//        }
//    }
//    @RequestMapping("login")
//    public String login(){
//        return "login";
//    }
//    @RequestMapping("logout")
//    public String logout(HttpSession session) {
//        session.invalidate();
//        return "login";
//    }
}
